app.controller("reportCtrl", ["$scope",'ngTableParams','datepickerPopupConfig', 'datepickerConfig',"ReportApi","$q","sweetAlertUtil",
    function($scope,ngTableParams,datepickerPopupConfig, datepickerConfig,ReportApi,$q,sweetAlertUtil) {
        

        $scope.memQueryParam = {
            page:1,//分页
            startDate:'',//开始时间
            endDate:'',//结束时间
            keyword:''//举办人电话
        }
        $scope.startDate = '' // 提交开始时间
        $scope.endDate = '' // 提交结束时间
        $scope.isInit = false;



        //------------------------start 初始化选择时间组件 -------------------------------
        initDatePicker()
        function initDatePicker() {
            datepickerConfig.minMode = "day"
            datepickerConfig.datepickerMode="day"
            datepickerConfig.showWeeks = false;
            datepickerPopupConfig.showButtonBar = false

            Date.prototype.format = function(format) {
                var o = {
                    "M+": this.getMonth() + 1, //month 
                    "d+": this.getDate(), //day 
                    "h+": this.getHours(), //hour 
                    "m+": this.getMinutes(), //minute 
                    "s+": this.getSeconds(), //second 
                    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
                    "S": this.getMilliseconds() //millisecond 
                }

                if (/(y+)/.test(format)) {
                    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                }

                for (var k in o) {
                    if (new RegExp("(" + k + ")").test(format)) {
                        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                    }
                }
                return format;
            }
        }
        //------------------------end 初始化选择时间组件 -------------------------------

        //------------------------start 获取列表信息 -------------------------------
        paginations()
        function getReportList() {
            var defer = $q.defer();
            var params = {
                page: $scope.memQueryParam.page,
                start: $scope.startDate,
                end:$scope.endDate,
                keyword:$scope.memQueryParam.keyword
            };
            ReportApi.getReportList(params).then(function(data){
                if (data.code != 200) {
                    sweetAlertUtil.fail(data.errMsg)
                    defer.reject(data.errMsg);
                    return;
                }
                $scope.resule = data.result.data;
                defer.resolve(data.result);
                $scope.isInit = true;
            })
            return defer.promise;
        }
        //------------------------end 获取列表信息 -------------------------------

        /*---------------------------start 分页开始 ------------------------------*/
        function paginations(res) {
            $scope.tableParams = new ngTableParams({
                page: 1, // show first page
                count: 10 // count per page
            }, {
                //total: 0, // length of data
                counts: [],
                getData: function ($defer, params) {
                    $scope.p_num = (params.page()-1)*10
                    $scope.memQueryParam['page'] = params.page();
                    getReportList().then(function (res) {
                        $scope.tableParams.total(res.total);
                        $defer.resolve(res.data);
                        $scope.isInit = true;
                    });
                }
            });
        }

        /*---------------------------end 分页结束 ------------------------------*/


        /*---------------------------start 搜索 ------------------------------*/
        $scope.searchDate = function(){
            $scope.startDate = $scope.memQueryParam.startDate!='' ? $scope.memQueryParam.startDate.format("yyyy-MM-dd") : ''
            $scope.endDate = $scope.memQueryParam.endDate!='' ? $scope.memQueryParam.endDate.format("yyyy-MM-dd") : '';

            $scope.tableParams.page(1);
            $scope.tableParams.reload();
            
        }
        /*---------------------------end 搜索 ------------------------------*/
    }
]);
